package cn.wuqingna.legendgem.gem

import cn.wuqingna.legendgem.gem.GemManager.getCacheGem
import cn.wuqingna.legendgem.gui.Gui
import cn.wuqingna.legendgem.gui.gemmanager.ProtectiveManager
import cn.wuqingna.legendgem.loader.GemLoader
import cn.wuqingna.legendgem.random.RandomData
import cn.wuqingna.legendgem.util.convertMapToStringMap
import cn.wuqingna.legendgem.util.randomInt
import org.bukkit.entity.Player
import org.bukkit.inventory.ItemStack
import taboolib.common.platform.function.adaptPlayer
import taboolib.common5.cbool
import taboolib.module.configuration.util.asMap
import taboolib.module.kether.KetherShell
import taboolib.module.kether.ScriptOptions
import taboolib.module.nms.ItemTagData
import taboolib.module.nms.getItemTag
import taboolib.platform.util.sendLang
import taboolib.platform.util.takeItem

class GemGroup(
    val metaLore: String,
    val gemCaches: MutableList<CacheGem> = mutableListOf(),
    var cacheLore: String = "",
    ){

    fun embed(gui: Gui, item: ItemStack, player: Player): Boolean{
        val cacheItem = item.getCacheGem()
        val gem = GemLoader.gems[cacheItem.key]!!
        val random = RandomData(player, convertMapToStringMap(cacheItem.map).toMutableMap())

        val probability = random.scriptStr(gem.options.embedProbability).toInt() + gui.luck

        if (gem.holePosition.any { cacheLore.contains(it) }) {
            if (randomInt(probability)) {
                gem.options.condition.embed.forEach {
                    if (KetherShell.eval(it.expression, ScriptOptions.new { sender(adaptPlayer(player)) }).get().cbool) {
                        gemCaches += cacheItem
                        it.consume?.let { it1 -> KetherShell.eval(it1, ScriptOptions.new { sender(adaptPlayer(player)) }) }
                        return true
                    } else {
                        player.sendLang(it.message)
                    }
                }
            } else {

                if (gem.options.broken){
                    if (gui.protective.isNotEmpty() && gem.options.protects.contains(gui.protective)){
                        ProtectiveManager.clear(gui)
                        player.inventory.takeItem(1) { it.isSimilar(gui.protectItem) }
                        player.sendLang("gem-embed-probability-clear")
                        return false
                    }
                    player.inventory.takeItem(1) { it.isSimilar(item) }
                    player.sendLang("gem-embed-probability")
                }

                return false
            }


        }

        return false
    }

    fun takeGem(gui: Gui, item: ItemStack, player: Player): Boolean{
        val tag = item.getItemTag()
        val key = tag.getDeep("legendGem.gem-key")!!.asString()
        val cache = tag.getDeep("legendGem.cache")!!.asMap()

        gemCaches.forEach {cacheGem ->
            val cacheMap = GemPlant.resetMaps(cacheGem.map)
            val map = ItemTagData.toNBT(cacheMap).asMap()
            cache.forEach { (t, u) ->
                if (!map.containsKey(t) && !map.containsValue(u)) {
                    return false
                }
            }

            if (cacheGem.key == key){
                val gem = GemLoader.gems[key]!!
                gem.options.condition.takeGem.forEach {
                    val probability = gem.options.takeProbability
                    val random  = RandomData(player, convertMapToStringMap(cacheMap).toMutableMap())
                    if (randomInt(random.scriptStr(probability).toInt() + gui.luck)) {
                        if (KetherShell.eval(it.expression, ScriptOptions.new { sender(adaptPlayer(player)) }).get().cbool) {
                            player.inventory.addItem(GemPlant.buildGem(player, cacheGem))
                            gemCaches.remove(cacheGem)
                            it.consume?.let { it1 -> KetherShell.eval(it1, ScriptOptions.new { sender(adaptPlayer(player)) }) }
                            return true
                        } else {
                            player.sendLang(it.message)
                        }
                    } else {
                        if (gem.options.broken){
                            if (gui.protective.isNotEmpty() && gem.options.protects.contains(gui.protective)){
                                ProtectiveManager.clear(gui)
                                player.inventory.takeItem { it.isSimilar(gui.protectItem) }
                                player.sendLang("gem-tack-probability-clear")
                                return false
                            }
                            player.inventory.takeItem { it.isSimilar(item) }
                            player.sendLang("gem-tack-probability")
                        }
                    }

                }
            }

        }
        return false
    }

    fun refreshLore(): String{
        var newLore = metaLore


        gemCaches.forEach { gemCache ->
            val gem = GemLoader.gems[gemCache.key]!!
            gem.holePosition.forEach { hole ->
                if (newLore.contains(hole))
                newLore = newLore.replaceFirst(hole, gem.icon)
            }
        }
        return newLore
    }

    fun refreshCacheLore(){
        cacheLore = refreshLore()
    }



}